package servlet;

import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AdminUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int aid=Integer.parseInt(request.getParameter("aid"));
        String name =request.getParameter("name");
        String sex=request.getParameter("sex");
        int age=Integer.parseInt(request.getParameter("age"));
        String tel=request.getParameter("tel");
        String address =request.getParameter("address");
        int wid=Integer.parseInt(request.getParameter("wid"));
        String sql ="UPDATE admin set `name`=?,sex=?,age=?,tel=?,address=?,wid=? WHERE aid=?";
        try {
            PreparedStatement ps = JdbcUtil.getConnection().prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, sex);
            ps.setInt(3,age);
            ps.setString(4, tel);
            ps.setString(5, address);
            ps.setInt(6,wid);
            ps.setInt(7,aid);
            int up = ps.executeUpdate();
            request.getRequestDispatcher("administrator.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
